#include "mainwindow.h"

#include <QTextEdit>
#include <QDebug>

#include "codeeditor.h"

MainWindow::MainWindow(QWidget *parent)
    : QSplitter(parent)
{
    InitializeUI();
}

void MainWindow::InitializeUI()
{
    edit_src_ = new CodeEditor(this);
    edit_display_ = new CodeEditor(this);

    edit_display_->setFontColor(QColor(163, 21, 21));

    auto lmd_make_sub = [](const QString& text) -> QString {
        QString ret;
        for (auto& c : text) {
            if (c != '"') {
                ret.push_back(c);
            }
            else {
                ret.push_back('\\');
                ret.push_back(c);
            }
        }
        return ret;
    };

    connect(edit_src_, &CodeEditor::textChanged, this, [&](){
        QString text = edit_src_->toPlainText();
        if (!text.isEmpty()) {
            QString display_text;
            QStringList list = text.split('\n');
            auto it = list.begin();
            while (it != list.end()) {
                display_text.push_back('"');
                display_text.append(lmd_make_sub(*it));
                ++it;
                if (it != list.end()) {
                    display_text.push_back("\\n");
                }
                display_text.push_back('"');
                if (it != list.end()) {
                    display_text.push_back('\n');
                }
            }
            edit_display_->document()->setPlainText(display_text);
        }
    });
}

MainWindow::~MainWindow()
{

}

